import Vue from 'vue';
import iView from 'iview';
import Vuex from 'vuex';
import VueRouter from 'vue-router';
import {routers,loginRouters,indexRouters} from './router';

import Util from './libs/util';
import App from './app.vue';
import 'iview/dist/styles/iview.css';
import 'bootstrap/dist/css/bootstrap.css'
import 'font-awesome/css/font-awesome.min.css'
import './views/main.css' //可复用的CSS 全局的CSS文件

Vue.use(VueRouter);
Vue.use(Vuex);

Vue.use(iView);//使用iView样式



// 路由配置
const RouterConfig = {
    mode: 'history',
    routes: routers
};
const router = new VueRouter(RouterConfig);

router.beforeEach((to, from, next) => {
    iView.LoadingBar.start();
    Util.title(to.meta.title);
    next();
});

router.afterEach(() => {
    iView.LoadingBar.finish();
    window.scrollTo(0, 0);
});
//全局状态管理
const store = new Vuex.Store({
  state: {
    //tab标签页
    tagList:[{name:'首页',icon:'home',href:'/dashboard'}],
    //当前页
    currentPageName:'',
    //当前页的路由路径
    currentPath:'',
    //当前页的面包屑路径
    breadPath:[],
    //左边侧边栏所有的list数据
    sidebarList:[]
  },
  mutations: {
    //判断tab页有没有点击的页面，没有的话push上去
    setSideBar (state,ele){
      var flag=-1;
      for(var i in state.tagList){
        if(state.tagList[i].name==ele.name){
          flag = 1;break;
        }
      }
      if(flag==-1){
        state.currentPath = ele.href;
        state.tagList.push(ele);
      }

    },
    setCurrentPage (state,currentPageName){
      localStorage.setItem('currentPageName', currentPageName);
      state.currentPageName = currentPageName;
      for(var index = 0;index<state.tagList.length;index++){
        if(state.tagList[index].name==currentPageName){
          state.currentPath = state.tagList[index].href;
        }
      }
    },
    //删除标签页
    deleteSideBar (state,index){
      state.tagList.splice(index,1);
    },
    sidebarList (state,data){
      state.sidebarList = data;
    },
    //生成面包屑
    setbreadNav (state,name){
      state.breadPath = [];
      for(var index=0;index<state.sidebarList.length;index++){
        if(state.sidebarList[index].children.length>0){
          for(var j = 0;j<state.sidebarList[index].children.length;j++){
            if(state.sidebarList[index].children[j].name == name){
              state.breadPath.push({name:state.sidebarList[index].name,href:''});
              state.breadPath.push({name:name,href:state.sidebarList[index].children[j].href});
            }
          }
        }else{
          if(state.sidebarList[index].name == name){
            state.breadPath.push({name:name,href:state.sidebarList[index].href});
          }
        }
      }
    }
  }
})
//挂载路由
new Vue({
    el: '#app',
    router: router,
    store: store,
    render: h => h(App)
});
